package com.zwz.service.impl;

import com.zwz.bean.User;
import com.zwz.controller.util.DESUtils;
import com.zwz.mapper.UserDao;
import com.zwz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DESUtils desUtils;

    //用户登录
    @Override
    public User login(User user) {
        System.out.println(user);
        User userDB = userDao.findByUserName(user.getUserAccount());
        System.out.println(userDB);
        if (!ObjectUtils.isEmpty(userDB)) {
            if (userDB.getUserPassword().equals(user.getUserPassword())) {
                return userDB;
            } else {
                throw new RuntimeException("密码输入不正确！");
            }
        } else {
            throw new RuntimeException("用户名输入错误！");
        }
    }

    //用户注册
    @Override
    public void register(User user) throws Exception {
        User userDB = userDao.findByUserName(user.getUserAccount());
        if (userDB == null) {
            user.setStatus("已激活");
            user.setRegisterTime(new Date());
            //密码加密
            String pwd = user.getUserPassword();
            user.setUserPassword(desUtils.encryptBaseDes(pwd));

            userDao.save(user);
        } else {
            throw new RuntimeException("用户名已存在！");
        }
    }
}




